package edu.ncu.controller;

import edu.ncu.entity.News;
import edu.ncu.entity.Photo;
import edu.ncu.entity.Publish;
import edu.ncu.entity.User;
import edu.ncu.service.NewsService;
import edu.ncu.service.PhotoService;
import edu.ncu.service.PublishService;
import edu.ncu.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class PageController {
    final UserService userService;
    final NewsService newsService;
    final PhotoService photoService;
    final PublishService publishService;

    public PageController(UserService userService, NewsService newsService, PhotoService photoService, PublishService publishService) {
        this.userService = userService;
        this.newsService = newsService;
        this.photoService = photoService;
        this.publishService = publishService;
    }

    @RequestMapping("/friendsRequests")
    public String friendsRequests()
    {
        return "person/friendsRequests";
    }

    @RequestMapping("/login")
    public String landingPage()
    {
        return "login";
    }

    @RequestMapping({"/", "/newsSquare_loggedOut"})
    public String newsSquare_loggedOut(Model model)
    {
        //查出热门用户
        List<User> hotUsers = userService.findAll();
        System.out.println("查出热门用户："+hotUsers);
        model.addAttribute("hotUsers", hotUsers);

        //查出热门动态
        List<Publish> publishes = publishService.getAllPublishes();
        model.addAttribute("publishes", publishes) ;

        //查出最新新闻
        List<News> hotNews = newsService.findAll();
        System.out.println("查出热门新闻："+hotNews);
        model.addAttribute("hotNews", hotNews);
        return "newsSquare_loggedOut";
    }



    @RequestMapping("/newsSquare")
    public String newsSquare(Model model)
    {
        //查出热门用户
        List<User> hotUsers = userService.findAll();
        System.out.println("查出热门用户："+hotUsers);
        model.addAttribute("hotUsers", hotUsers);

        //查出热门动态
        List<Publish> publishes = publishService.getAllPublishes();
        model.addAttribute("publishes", publishes) ;

        //查出最新新闻
        List<News> hotNews = newsService.findAll();
        System.out.println("查出热门新闻："+hotNews);
        model.addAttribute("hotNews", hotNews);
        return "newsSquare";
    }

    @RequestMapping("/newsDetail")
    public String newsDetail()
    {
        return "newsDetail";
    }



    @RequestMapping("/admin")
    public String admin()
    {
        return "admin";
    }

    @RequestMapping("/error")
    public String error()
    {
        return "error";
    }

    @RequestMapping("/friends")
    public String friends()
    {
        return "friends";
    }

    @RequestMapping("/friends2")
    public String friends2()
    {
        return "friends2";
    }

    @RequestMapping("/friendsDel")
    public String friendsDel()
    {
        return "friendsDel";
    }

    @RequestMapping("/friendsPage")
    public String friendsPage()
    {
        return "friendsPage";
    }

    @RequestMapping("/index")
    public String main()
    {
        return "index";
    }

    @RequestMapping("/login2")
    public String login()
    {
        return "login2";
    }

    @RequestMapping("/news")
    public String news()
    {
        return "news";
    }

    @RequestMapping("/photos")
    public String photos()
    {
        return "photos";
    }

    @RequestMapping("/photosPage")
    public String photosPage()
    {
        return "photosPage";
    }

    @RequestMapping("/profilePage")
    public String profilePage(int userId, Model model)
    {
        //找出个人资料
        User byUserId = userService.findByUserId(userId);
        System.out.println("找出的个人资料："+byUserId);
        model.addAttribute("user", byUserId);

        //找出最近照片
        List<Photo> photos = photoService.findByUserId(userId);
        System.out.println("找出最近照片："+photos);
        model.addAttribute("photos", photos);

        //找出最近动态
        List<Publish> publishes = publishService.getPublishes(userId);
        model.addAttribute("publishes", publishes);
        return "profilePage";
    }

    //个人资料
    @RequestMapping("/personDetail")
    public String personDetail()
    {
        return "person/personDetail";
    }


}
